﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;

namespace FreeBroker
{
    public class ChartController
    {
        public const string OPT_TIMEPERIOD = "Zeitraum";
        public const string OPT_CHARTTYPE = "Chart Typ";
        public const string OPT_CHARTCALC = "Chart Berechnung";
        public const string OPT_CHARTVALUE = "Chart Wert";
        public const string OPT_AVERAGE1 = "Durchschnitt Wert 1";
        public const string OPT_AVERAGE2 = "Durchschnitt Wert 2";
        public const string OPT_AVERAGE3 = "Durchschnitt Wert 3";
        public const string OPT_AVERAGETYPE = "Durchschnitt Typ";
        public const string OPT_COMPARISON = "Vergleich";
        public const string OPT_CHARTINDICATOR = "Interner Indikator";
        public const string OPT_EXTRAINDICATOR1 = "Externer Indikator 1";
        public const string OPT_EXTRAINDICATOR2 = "Externer Indikator 2";
        public const string OPT_EXTRAINDICATOR3 = "Externer Indikator 3";
        public const string OPT_EXTRAINDICATOR4 = "Externer Indikator 4";
        public const string OPT_EXTRAINDICATOR5 = "Externer Indikator 5";
        public const string OPT_EXTRAINDICATOR6 = "Externer Indikator 6";

        public static Dictionary<string, Dictionary<string, string>> OPTIONS = new Dictionary<string, Dictionary<string, string>>();
        public static Dictionary<string, string> TIMESPANS = new Dictionary<string, string>();
        public static Dictionary<string, string> CHART_TYPES = new Dictionary<string, string>();
        public static Dictionary<string, string> CHART_CALCULATIONS = new Dictionary<string, string>();
        public static Dictionary<string, string> CHART_VALUES = new Dictionary<string, string>();
        public static Dictionary<string, string> AVERAGE_VALUES = new Dictionary<string, string>();
        public static Dictionary<string, string> AVERAGE_TYPES = new Dictionary<string, string>();
        public static Dictionary<string, string> COMPARISONS = new Dictionary<string, string>();
        public static Dictionary<string, string> CHART_INDICATORS = new Dictionary<string, string>();
        public static Dictionary<string, string> EXTRA_INDICATORS = new Dictionary<string, string>();
        static ChartController() {
            // SET OPTIONS
            OPTIONS.Add(OPT_TIMEPERIOD, TIMESPANS);
            OPTIONS.Add(OPT_CHARTTYPE, CHART_TYPES);
            OPTIONS.Add(OPT_CHARTCALC, CHART_CALCULATIONS);
            OPTIONS.Add(OPT_CHARTVALUE, CHART_VALUES);
            OPTIONS.Add(OPT_AVERAGE1, AVERAGE_VALUES);
            OPTIONS.Add(OPT_AVERAGE2, AVERAGE_VALUES);
            OPTIONS.Add(OPT_AVERAGE3, AVERAGE_VALUES);
            OPTIONS.Add(OPT_AVERAGETYPE, AVERAGE_TYPES);
            OPTIONS.Add(OPT_COMPARISON, COMPARISONS);
            OPTIONS.Add(OPT_CHARTINDICATOR, CHART_INDICATORS);
            OPTIONS.Add(OPT_EXTRAINDICATOR1, EXTRA_INDICATORS);
            OPTIONS.Add(OPT_EXTRAINDICATOR2, EXTRA_INDICATORS);
            OPTIONS.Add(OPT_EXTRAINDICATOR3, EXTRA_INDICATORS);
            OPTIONS.Add(OPT_EXTRAINDICATOR4, EXTRA_INDICATORS);
            OPTIONS.Add(OPT_EXTRAINDICATOR5, EXTRA_INDICATORS);
            OPTIONS.Add(OPT_EXTRAINDICATOR6, EXTRA_INDICATORS);
            //
            CHART_TYPES.Add("Linienchart", "CONNECTLINE");
            CHART_TYPES.Add("Unterbrochener Linienchart", "line");
            CHART_TYPES.Add("Balkenchart", "hilow");
            CHART_TYPES.Add("Open/High/Low/Close", "ohlc");
            CHART_TYPES.Add("Candlestick", "candle");
            //
            CHART_CALCULATIONS.Add("linear", "lin");
            CHART_CALCULATIONS.Add("logarithmisch", "log");
            //
            CHART_VALUES.Add("Preis", "abs");
            CHART_VALUES.Add("Prozent", "rel");
            //
            AVERAGE_VALUES.Add("20", "20");
            AVERAGE_VALUES.Add("38", "38");
            AVERAGE_VALUES.Add("50", "50");
            AVERAGE_VALUES.Add("100", "100");
            AVERAGE_VALUES.Add("150", "150");
            AVERAGE_VALUES.Add("200", "200");
            //
            AVERAGE_TYPES.Add("einfach", "simple");
            AVERAGE_TYPES.Add("exponentiell", "exp");
            //
            COMPARISONS.Add("Kein", "");
            COMPARISONS.Add("DAX", "DAX.ETR");
            COMPARISONS.Add("TecDAX", "TDXP.ETR");
            COMPARISONS.Add("MDAX", "MDAX.ETR");
            COMPARISONS.Add("Euro Stoxx 50", "SX5E.DJX");
            COMPARISONS.Add("FTSE 100", "UKX.ISE");
            COMPARISONS.Add("CAC 40", "PCAC.PSE");
            COMPARISONS.Add("MIB 30", "930.AFF");
            COMPARISONS.Add("Dow Jones 30", "DJI.DJI");
            COMPARISONS.Add("Nasdaq Comp.", "COMPX.NQI");
            COMPARISONS.Add("Nasdaq 100", "NDX.X.NQI");
            COMPARISONS.Add("S&P 500", "INX.SPI");
            COMPARISONS.Add("Nikkei 225", "N225.FX1");
            COMPARISONS.Add("HangSeng", "HKHI.TWI");
            COMPARISONS.Add("MSCI World", "990100-STRD-UHD-EUR.MSI");
            COMPARISONS.Add("EUR/USD", "DUSDEUR.TGT");
            COMPARISONS.Add("EUR/JPY", "DJPYEUR.TGT");
            COMPARISONS.Add("USD/JPY", "DJPYUSD.TGT");
            COMPARISONS.Add("3% Zinsen im Jahr", "3%25");
            COMPARISONS.Add("5% Zinsen im Jahr", "5%25");
            COMPARISONS.Add("8% Zinsen im Jahr", "8%25");
            COMPARISONS.Add("10% Zinsen im Jahr", "10%25");
            COMPARISONS.Add("15% Zinsen im Jahr", "15%25");
            COMPARISONS.Add("20% Zinsen im Jahr", "20%25");
            COMPARISONS.Add("VDAX", "VDAX.DTB");
            COMPARISONS.Add("REX", "REXP.FSE");
            //
            CHART_INDICATORS.Add("Kein", "");
            CHART_INDICATORS.Add("Bollinger Bands", "BB");
            CHART_INDICATORS.Add("Envelepes", "ENV");
            CHART_INDICATORS.Add("ZigZag Basic", "ZIGZAG");
            //
            EXTRA_INDICATORS.Add("Kein", "");
            EXTRA_INDICATORS.Add("Volumen rot/grün", "VOLUME");
            EXTRA_INDICATORS.Add("Volumen rot/blau", "VOLUME_BLUERED");
            EXTRA_INDICATORS.Add("Volumen alle Daten", "VOLUME_ALL");
            EXTRA_INDICATORS.Add("Momentum", "MOM");
            EXTRA_INDICATORS.Add("RSI", "RSI");
            EXTRA_INDICATORS.Add("MACD", "MACD");
            EXTRA_INDICATORS.Add("Stochastik - slow", "SST");
            EXTRA_INDICATORS.Add("Stochastik - fast", "FST");
            EXTRA_INDICATORS.Add("Dividende", "DIVIDENDE");
            EXTRA_INDICATORS.Add("Preis/Gewinn", "PREISGEWINN");
            EXTRA_INDICATORS.Add("Accumulation-Dist.Line", "ADLINE");
            EXTRA_INDICATORS.Add("Average Directional Index", "ADX");
            EXTRA_INDICATORS.Add("Aroon Up/Down", "AROON");
            EXTRA_INDICATORS.Add("Aroon Oscillator", "AROSC");
            EXTRA_INDICATORS.Add("Average True Range", "ATR");
            EXTRA_INDICATORS.Add("Commodity Channel Index", "CCI");
            EXTRA_INDICATORS.Add("Chaikin Money Flow", "CMF");
            EXTRA_INDICATORS.Add("Chaikin Oscillator", "COS");
            EXTRA_INDICATORS.Add("Directional Movement Index", "DMI");
            EXTRA_INDICATORS.Add("Money Flow Index", "MFI");
            EXTRA_INDICATORS.Add("Negative Volume Index", "NVI");
            EXTRA_INDICATORS.Add("Overbought/Oversold", "OBOS");
            EXTRA_INDICATORS.Add("On Balance Volume", "OBV");
            EXTRA_INDICATORS.Add("Positive Volume Index", "PVI");
            EXTRA_INDICATORS.Add("Rate Of Change", "ROC");
            EXTRA_INDICATORS.Add("Stochastic RSI", "SRS");
            EXTRA_INDICATORS.Add("Trendbestätigungsindikator", "TBI");
            EXTRA_INDICATORS.Add("Ultimate Oscillator", "ULTOSC");
            EXTRA_INDICATORS.Add("Vertical Horizontal Filter", "VHF");
            EXTRA_INDICATORS.Add("Volume Price Trend", "VPT");
            EXTRA_INDICATORS.Add("Williams %R", "WILLIAMS");
            //
            TIMESPANS.Add("Intraday", "1d");
            TIMESPANS.Add("5 Tage", "5d");
            TIMESPANS.Add("10 Tage", "10d");
            TIMESPANS.Add("3 Monate", "3m");
            TIMESPANS.Add("6 Monate", "6m");
            TIMESPANS.Add("1 Jahr", "1y");
            TIMESPANS.Add("5 Jahre", "5y");
         }

        public static string buildRequest(BrokerObject broker)
        {
            if (broker == null || broker.chartOptions == null)
            {
                return null;
            }
            if (broker.IdNotation == null || broker.IdNotation.Length == 0)
            {
                Console.WriteLine("No notation ID defined !");
                return null;
            }
            StringBuilder builder = new StringBuilder("http://charts.comdirect.de/charts/large.chart");
            builder.Append("?WIDTH=632&HEIGHT=614");

            // When Time_Span is set to "range" FROM must be set
            // UNIX Timestamp (in seconds)
            //builder.Append("&FROM=" + Helper.dateTime2Timestamp(DateTime.Now) / 1000));

            // UNIX Timestamp (in seconds)

            builder.Append("&TO=" + Helper.dateTime2Timestamp(DateTime.Now) / 1000);
            if (broker.chartOptions.ContainsKey(OPT_CHARTTYPE))
            {
                builder.Append("&TYPE=" + broker.chartOptions[OPT_CHARTTYPE]);
            }
            if (broker.chartOptions.ContainsKey(OPT_TIMEPERIOD))
            {
                builder.Append("&TIME_SPAN=" + broker.chartOptions[OPT_TIMEPERIOD]);
            }
            if (broker.chartOptions.ContainsKey(OPT_CHARTCALC))
            {
                builder.Append("&AXIS_SCALE=" + broker.chartOptions[OPT_CHARTCALC]);
            }
            if (broker.chartOptions.ContainsKey(OPT_CHARTVALUE))
            {
                builder.Append("&DATA_SCALE=" + broker.chartOptions[OPT_CHARTVALUE]);
            }
            // If more notations shall be combined use a "+" and append next notation
            // -> LCOLORS must contain additional values too, also appended with "+"
            // Colors are written as hex RGB, e.g.:  3366cc
            builder.Append("&LNOTATIONS=" + broker.IdNotation);
            foreach (string notation in broker.AdditionalNotations.Keys)
            {
                builder.Append("+").Append(notation);
            }
            builder.Append("&LCOLORS=000000");
            foreach (Color color in broker.AdditionalNotations.Values)
            {
                builder.Append("+");
                builder.Append(color.R.ToString("X2"));
                builder.Append(color.G.ToString("X2"));
                builder.Append(color.B.ToString("X2"));
            }
            //
            if (broker.chartOptions.ContainsKey(OPT_AVERAGE1))
            {
                builder.Append("&AVG1=" + broker.chartOptions[OPT_AVERAGE1]);
            }
            if (broker.chartOptions.ContainsKey(OPT_AVERAGE2))
            {
                builder.Append("&AVG2=" + broker.chartOptions[OPT_AVERAGE2]);
            }
            if (broker.chartOptions.ContainsKey(OPT_AVERAGE3))
            {
                builder.Append("&AVG3=" + broker.chartOptions[OPT_AVERAGE3]);
            }
            if (broker.chartOptions.ContainsKey(OPT_AVERAGETYPE))
            {
                builder.Append("&AVGTYPE=" + broker.chartOptions[OPT_AVERAGETYPE]);
            }
            if (broker.chartOptions.ContainsKey(OPT_CHARTINDICATOR))
            {
                builder.Append("&IND=" + broker.chartOptions[OPT_CHARTINDICATOR]);
            }
            if (broker.chartOptions.ContainsKey(OPT_EXTRAINDICATOR1))
            {
                builder.Append("&IND0=" + broker.chartOptions[OPT_EXTRAINDICATOR1]);
            }
            if (broker.chartOptions.ContainsKey(OPT_EXTRAINDICATOR2))
            {
                builder.Append("&IND1=" + broker.chartOptions[OPT_EXTRAINDICATOR2]);
            }
            if (broker.chartOptions.ContainsKey(OPT_EXTRAINDICATOR3))
            {
                builder.Append("&IND2=" + broker.chartOptions[OPT_EXTRAINDICATOR3]);
            }
            if (broker.chartOptions.ContainsKey(OPT_EXTRAINDICATOR4))
            {
                builder.Append("&IND3=" + broker.chartOptions[OPT_EXTRAINDICATOR4]);
            }
            if (broker.chartOptions.ContainsKey(OPT_EXTRAINDICATOR5))
            {
                builder.Append("&IND4=" + broker.chartOptions[OPT_EXTRAINDICATOR5]);
            }
            if (broker.chartOptions.ContainsKey(OPT_EXTRAINDICATOR6))
            {
                builder.Append("&IND5=" + broker.chartOptions[OPT_EXTRAINDICATOR6]);
            }
            builder.Append("&WITH_EARNINGS=1");
            builder.Append("&SHOWHL=1");
            return builder.ToString();
        }

    }
}
